package com.iotmonitor.iot.service.impl;

import com.google.common.collect.Lists;
import com.iotmonitor.iot.domain.dto.*;
import com.iotmonitor.iot.es.ESRepository;
import com.iotmonitor.iot.mapper.TbBoardMapper;
import com.iotmonitor.iot.mapper.TdEngineMapper;
import com.iotmonitor.iot.service.TbReportService;
import com.iotmonitor.iot.domain.TbBoard;
import com.iotmonitor.iot.domain.vo.BoardQuotaData;
import com.iotmonitor.iot.domain.vo.BoardQuotaVO;
import com.iotmonitor.iot.domain.vo.PieVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TbReportServiceImpl implements TbReportService {

    @Autowired
    private ESRepository esRepository;

    @Autowired
    private TbBoardMapper boardMapper;

    @Autowired
    private TdEngineMapper tdEngineMapper;

    @Override
    public List<PieVO> getStatusCollect() {

        Long allDeviceCount = esRepository.getAllDeviceCount();//全部设备数量

        Long offlineCount = esRepository.getOfflineCount();//离线设备数量

        Long alarmCount = esRepository.getAlarmCount();//报警设备数量


        PieVO devicePie=new PieVO();
        devicePie.setName("正常运转");
        devicePie.setValue(allDeviceCount-offlineCount-alarmCount);

        PieVO offlinePie=new PieVO();
        offlinePie.setName("离线");
        offlinePie.setValue(offlineCount);

        PieVO alarmPie =new PieVO();
        alarmPie.setName("报警");
        alarmPie.setValue(alarmCount);

        List<PieVO> pieVOList= Lists.newArrayList();
        pieVOList.add(devicePie);
        pieVOList.add(offlinePie);
        pieVOList.add(alarmPie);

        return pieVOList;
    }


    @Override
    public List<TrendPoint> getAlarmTrend(String start, String end, int type) {
        String typeName = null;
        if(type==1){
            typeName = "1m";
        }
        if(type==2){
            typeName = "1h";
        }
        if(type==3){
            typeName = "1d";
        }
        
        List<TrendPoint> trendPointList = tdEngineMapper.getAlarmTrend(start,end,typeName);

        return trendPointList;
    }

    @Override
    public List<HeapPoint> getTop10Alarm(String startTime, String endTime) {
        List<HeapPoint> heapPointList = tdEngineMapper.getTop10Alarm(startTime, endTime);
        return heapPointList;
    }

    @Override
    public List<String> getDeviceByQuota(String quotaId) {

        List<QuotaInfo> quotaInfoList = tdEngineMapper.getDeviceByQuota(quotaId);

        //设备id列表
        List<String> deviceIdList = quotaInfoList.stream().map(quotaInfo -> quotaInfo.getDeviceId()).collect(Collectors.toList());

        return deviceIdList;
    }

    @Override
    public List<TrendPoint2> getQuotaTrend(String startTime, String endTime, String quotaId, String deviceId, int type) {

        String typeName = null;
        if(type==1){ //1小时
            typeName = "1m";
        }
        if(type==2){  //1天
            typeName = "1h";
        }
        if(type==3){ //1天
            typeName = "1d";
        }

        List<TrendPoint2> trendPoint2List = tdEngineMapper.getQuotaTrend(startTime,endTime,quotaId,deviceId,typeName);

        return replenish(trendPoint2List);
    }

    @Override
    public BoardQuotaVO getBoardData(String quotaId, List<String> deviceIds, String startTime, String endTime, Integer type) {
        //参数校验
        if( quotaId==null || deviceIds==null || deviceIds.size()==0 ){
            return new BoardQuotaVO();
        }

        BoardQuotaVO boardQuotaVO=new BoardQuotaVO();
        boardQuotaVO.setSeries(  Lists.newArrayList());

        for( String deviceId:deviceIds ){  //循环每个设备
            //每个设备的指标趋势
            List<TrendPoint2> trendPoint2List = getQuotaTrend(startTime, endTime, quotaId, deviceId, type);

            //x轴
            if(boardQuotaVO.getXdata()==null){
                boardQuotaVO.setXdata(  trendPoint2List.stream().map(trendPoint2 -> trendPoint2.getTime()  ).collect( Collectors.toList() ) );
            }

            //数据
            BoardQuotaData boardQuotaData=new BoardQuotaData();
            boardQuotaData.setName( deviceId );
            boardQuotaData.setData( trendPoint2List.stream().map( trendPoint2 -> trendPoint2.getPointValue() ).collect(Collectors.toList())  );

            boardQuotaVO.getSeries().add(boardQuotaData);

        }


        return boardQuotaVO;
    }


    /**
     * 填充数据
     * @param trendPoint2List
     * @return
     */
    private List<TrendPoint2> replenish(List<TrendPoint2> trendPoint2List){

        Double  previousValue=null;// 上一个值
        for(TrendPoint2 trendPoint2: trendPoint2List ){
            if(trendPoint2.getPointValue()!=null){
                previousValue=trendPoint2.getPointValue();
                break;
            }
        }
        if(previousValue==null){
            previousValue=0d;
        }

        //数据填充逻辑
        for( TrendPoint2 trendPoint2: trendPoint2List){
            if(trendPoint2.getPointValue()==null){
                trendPoint2.setPointValue(previousValue);
            }
            previousValue=trendPoint2.getPointValue();
        }
        return trendPoint2List;
    }



    @Override
    public BoardQuotaVO getBoardData(Long id, String start, String end, Integer type) {
        //从mysql中获取面板实体数据
        TbBoard boardEntity = boardMapper.selectTbBoardById(id);
        if(boardEntity == null) {
            return null;
        }

        List<String> deviceIdList = Arrays.asList(boardEntity.getDevice().split(","));
        BoardQuotaVO boardQuotaVO  = getBoardData(boardEntity.getQuota() + "",deviceIdList,start,end,type);
        boardQuotaVO.setName(boardEntity.getName());

        return boardQuotaVO;
    }

}
